package com.demo.reservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

public abstract class BaseController<T> {
    private IService<T> s;

    protected BaseController(IService<T> s){
        this.s = s;
    }

    @RequestMapping("/{id}")
    public T queryById(@PathVariable("id") Serializable id) {
        return s.getById(id);
    }

    @RequestMapping("/all")
    public IPage<T> queryAll(@RequestParam(value = "page_size",defaultValue = "1")int page,@RequestParam(value = "page_size",defaultValue = "15") int pageSize){
        return s.getBaseMapper().selectPage(Page.of(page,pageSize),null);
    }

    @PutMapping("/")
    public T update(T t) {
        if(!s.updateById(t)) throw new RuntimeException("更新数据失败");
        return t;
    }

    @PostMapping("/")
    public T create(T t) {
        if(!s.save(t))  throw new RuntimeException("保存数据失败");
        return t;
    }
}
